package com.huikeportal.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huikeportal.dao.FootfallGlobalMapper;
import com.huikeportal.dao.TodayIndexesMapper;
import com.huikeportal.model.SiteAllInfo;
import com.huikeportal.model.TodayIndexes;
import com.huikeportal.service.FootfallGlobalService;
import com.huikeportal.service.TodayIndexesService;

@Service("todayIndexesService")
public class TodayIndexesServiceImpl implements TodayIndexesService {

	@Autowired
	private TodayIndexesMapper todayIndexesMapper;
	
	@Autowired
	private FootfallGlobalMapper footfallGlobalMapper;
	
	@Autowired
	private FootfallGlobalService footfallGlobalService;
	public HashMap<String, Object> getTodayIndexes(String siteId,List<String> indexListCurrent) throws Exception {
		
		List<String> indexList = indexListCurrent;
//		List<String> indexList = new ArrayList<String>();
//		indexList.add("1");
//		indexList.add("3");
//		indexList.add("7");
		List<HashMap<String, Object>> indexDataList = new ArrayList<HashMap<String,Object>>();
		for(int i = 0;i<indexList.size();i++){
			HashMap<String,Object>  calret= footfallGlobalService.getCalret(Integer.parseInt(indexList.get(i)));
			String index = footfallGlobalService.getOrderField(indexList.get(i));
			String indexName = footfallGlobalService.getOrderFieldName(indexList.get(i));
			HashMap<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("index", index);
			paraMap.put("siteId", siteId);
			paraMap.put("calret", calret.get("calret"));
			paraMap.put("calretC", calret.get("calretC"));
			paraMap.put("calretD", calret.get("calretD"));
			String  dayData  = todayIndexesMapper.getIndexCurDayData(paraMap);
			String  lastDayData  = todayIndexesMapper.getIndexCurLastDayData(paraMap);
			String  lastWeekData  = todayIndexesMapper.getIndexCurLastWeekData(paraMap);
			String  lastDay  = todayIndexesMapper.getIndexCurLastDay(paraMap);//昨天同一时间
			String  lastWeek  = todayIndexesMapper.getIndexCurLastWeek(paraMap);//上周同一时间
			
			float lastDayFloat = lastDay !=null?Float.parseFloat(lastDay):0;
			float lastWeekFloat = lastWeek !=null?Float.parseFloat(lastWeek):0;
			float dayDataFloat = dayData !=null?Float.parseFloat(dayData):0;
			String comLastDay = "";
			String comLastWeekDay = "";
			if(lastDayFloat!=0){
				if(lastDayFloat>dayDataFloat){
					comLastDay = comLastDay+"-";
				}else{
					comLastDay = comLastDay+"+";
				}
				int per1 = (int) ((dayDataFloat-lastDayFloat)*100/lastDayFloat);
				if(per1<0){
					per1 = 0 - per1;
				}
				comLastDay = comLastDay+String.valueOf(per1);
			}else{
				comLastDay ="--";
			}
			if(lastWeekFloat!=0){
				if(lastWeekFloat>dayDataFloat){
					comLastWeekDay = comLastWeekDay+"-";
				}else{
					comLastWeekDay = comLastWeekDay+"+";
				}
				int per1 = (int) ((dayDataFloat-lastWeekFloat)*100/lastWeekFloat);
				if(per1<0){
					per1 = 0 - per1;
				}
				comLastWeekDay = comLastWeekDay+String.valueOf(per1);
			}else{
				comLastWeekDay ="--";
			}
			
			TodayIndexes IndexesData = new TodayIndexes();
			if(dayData!=null&&!"".equals(dayData)){
				IndexesData.setDayData(dayData);
			}else{
				IndexesData.setDayData("0");
			}
			if(lastDayData!=null&&!"".equals(lastDayData)){
				IndexesData.setLastDayData(lastDayData);
			}else{
				IndexesData.setLastDayData("0");
			}
			if(lastWeekData!=null&&!"".equals(lastWeekData)){
				IndexesData.setLastWeekData(lastWeekData);
			}else{
				IndexesData.setLastWeekData("0");
			}
			
			if(IndexesData == null){
				IndexesData =  new TodayIndexes();
				IndexesData.init();
			}
			HashMap<String, Object> result = new HashMap<String, Object>();
			result.put("indexNum", indexList.get(i));
			result.put("index", index);
			result.put("indexName", indexName);
			result.put("IndexesData", IndexesData);
			result.put("comLastDay", comLastDay);
			result.put("comLastWeekDay", comLastWeekDay);
			indexDataList.add(result);
		}
		TodayIndexes todayIndexes=new TodayIndexes();
		todayIndexes.setSiteId(siteId);
		TodayIndexes todayIndexesWeather=todayIndexesMapper.getWeather(todayIndexes);
		todayIndexes.setTodayWeather(todayIndexesWeather.getTodayWeather());
		todayIndexes.setLastDayWeather(todayIndexesWeather.getLastDayWeather());
		todayIndexes.setLastWeekWeather(todayIndexesWeather.getLastWeekWeather());
		HashMap<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("indexDataList", indexDataList);
		resultMap.put("todayIndexes", todayIndexes);
		return resultMap;
		
		/**
		 * 
		 
		TodayIndexes todayIndexes=new TodayIndexes();
		todayIndexes.setSiteId(siteId);
		try{
		TodayIndexes todayIndexesTraffic=todayIndexesMapper.getTraffic(todayIndexes);
		TodayIndexes todayIndexesPos=todayIndexesMapper.getPos(todayIndexes);
		TodayIndexes todayIndexesPv=todayIndexesMapper.getPv(todayIndexes);
		TodayIndexes todayIndexesUv=todayIndexesMapper.getUv(todayIndexes);
		TodayIndexes todayIndexesWeather=todayIndexesMapper.getWeather(todayIndexes);
		if(todayIndexesTraffic!=null){
			todayIndexes.setTraffic(todayIndexesTraffic.getTraffic());
			todayIndexes.setLastDayTraffic(todayIndexesTraffic.getLastDayTraffic());
			todayIndexes.setLastWeekTraffic(todayIndexesTraffic.getLastWeekTraffic());
		}
		else
		{
			todayIndexes.setTraffic(0);
			todayIndexes.setLastDayTraffic(0);
			todayIndexes.setLastWeekTraffic(0);
		}
		if(todayIndexesPos==null){
			todayIndexes.setPos(0);
			todayIndexes.setLastDayPos(0);
			todayIndexes.setLastWeekPos(0);
		}
		else
		{
			todayIndexes.setPos(todayIndexesPos.getPos());
			todayIndexes.setLastDayPos(todayIndexesPos.getLastDayPos());
			todayIndexes.setLastWeekPos(todayIndexesPos.getLastWeekPos());
		}
		if(todayIndexesPv==null){
			todayIndexes.setSinglePv(0);
			todayIndexes.setLastDaySinglePv(0);
			todayIndexes.setLastWeekSinglePv(0);
		}
		else{
			todayIndexes.setSinglePv(todayIndexesPv.getSinglePv());
			todayIndexes.setLastDaySinglePv(todayIndexesPv.getLastDaySinglePv());
			todayIndexes.setLastWeekSinglePv(todayIndexesPv.getLastWeekSinglePv());
		}
		if(todayIndexesUv==null){
			todayIndexes.setSigleUv(0);
			todayIndexes.setLastDaySigleUv(0);
			todayIndexes.setLastWeekSigleUv(0);
		}
		else{
			todayIndexes.setSigleUv(todayIndexesUv.getSigleUv());
			todayIndexes.setLastDaySigleUv(todayIndexesUv.getLastDaySigleUv());
			todayIndexes.setLastWeekSigleUv(todayIndexesUv.getLastWeekSigleUv());
		}
		
		todayIndexes.setTodayWeather(todayIndexesWeather.getTodayWeather());
		todayIndexes.setLastDayWeather(todayIndexesWeather.getLastDayWeather());
		todayIndexes.setLastWeekWeather(todayIndexesWeather.getLastWeekWeather());
		return todayIndexes;*/
	}
	
	public HashMap<String, Object> getTodayIndexes(String siteId,String userId,List<String> indexListCurrent) throws Exception{

		//指标
		List<String> indexList = indexListCurrent;
//		List<String> indexList = new ArrayList<String>();
//		indexList.add("1");
//		indexList.add("3");
//		indexList.add("7");
		List<HashMap<String, Object>> indexDataList = new ArrayList<HashMap<String,Object>>();
		for(int i = 0;i<indexList.size();i++){
			HashMap<String,Object>  calret= footfallGlobalService.getCalret(Integer.parseInt(indexList.get(i)));
			String index = footfallGlobalService.getOrderField(indexList.get(i));
			String indexName = footfallGlobalService.getOrderFieldName(indexList.get(i));
			HashMap<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("index", index);
			paraMap.put("userId", userId);
			paraMap.put("siteId", siteId);
			paraMap.put("calret", calret.get("calret"));
			paraMap.put("calretC", calret.get("calretC"));
			paraMap.put("calretD", calret.get("calretD"));
			String  dayData  = todayIndexesMapper.getIndexPartCurDayData(paraMap);
			String  lastDayData  = todayIndexesMapper.getIndexPartCurLastDayData(paraMap);
			String  lastWeekData  = todayIndexesMapper.getIndexPartCurLastWeekData(paraMap);
			String  lastDay  = todayIndexesMapper.getIndexPartCurLastDay(paraMap);//昨天同一时间
			String  lastWeek  = todayIndexesMapper.getIndexPartCurLastWeek(paraMap);//上周同一时间
			
			float lastDayFloat = lastDay !=null?Float.parseFloat(lastDay):0;
			float lastWeekFloat = lastWeek !=null?Float.parseFloat(lastWeek):0;
			float dayDataFloat = dayData !=null?Float.parseFloat(dayData):0;
			String comLastDay = "";
			String comLastWeekDay = "";
			if(lastDayFloat!=0){
				if(lastDayFloat>dayDataFloat){
					comLastDay = comLastDay+"-";
				}else{
					comLastDay = comLastDay+"+";
				}
				int per1 = (int) ((dayDataFloat-lastDayFloat)*100/lastDayFloat);
				if(per1<0){
					per1 = 0 - per1;
				}
				comLastDay = comLastDay+String.valueOf(per1);
			}else{
				comLastDay ="--";
			}
			if(lastWeekFloat!=0){
				if(lastWeekFloat>dayDataFloat){
					comLastWeekDay = comLastWeekDay+"-";
				}else{
					comLastWeekDay = comLastWeekDay+"+";
				}
				int per1 = (int) ((dayDataFloat-lastWeekFloat)*100/lastWeekFloat);
				if(per1<0){
					per1 = 0 - per1;
				}
				comLastWeekDay = comLastWeekDay+String.valueOf(per1);
			}else{
				comLastWeekDay ="--";
			}
			
			
			TodayIndexes IndexesData = new TodayIndexes();
			if(dayData!=null&&!"".equals(dayData)){
				IndexesData.setDayData(dayData);
			}else{
				IndexesData.setDayData("0");
			}
			if(lastDayData!=null&&!"".equals(lastDayData)){
				IndexesData.setLastDayData(lastDayData);
			}else{
				IndexesData.setLastDayData("0");
			}
			if(lastWeekData!=null&&!"".equals(lastWeekData)){
				IndexesData.setLastWeekData(lastWeekData);
			}else{
				IndexesData.setLastWeekData("0");
			}
			
			if(IndexesData == null){
				IndexesData =  new TodayIndexes();
				IndexesData.init();
			}
			HashMap<String, Object> result = new HashMap<String, Object>();
			result.put("indexNum", indexList.get(i));
			result.put("index", index);
			result.put("indexName", indexName);
			result.put("IndexesData", IndexesData);
			result.put("comLastDay", comLastDay);
			result.put("comLastWeekDay", comLastWeekDay);
			indexDataList.add(result);
		}
		//天气
		TodayIndexes todayIndexes=new TodayIndexes();
		todayIndexes.setSiteId(siteId);
		TodayIndexes todayIndexesWeather=todayIndexesMapper.getWeather(todayIndexes);
		todayIndexes.setTodayWeather(todayIndexesWeather.getTodayWeather());
		todayIndexes.setLastDayWeather(todayIndexesWeather.getLastDayWeather());
		todayIndexes.setLastWeekWeather(todayIndexesWeather.getLastWeekWeather());
		HashMap<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("indexDataList", indexDataList);
		resultMap.put("todayIndexes", todayIndexes);
		return resultMap;
		/**
		 * 
		TodayIndexes todayIndexes=new TodayIndexes();
		todayIndexes.setSiteId(siteId);
		todayIndexes.setUserId(userId);
		try{
		TodayIndexes todayIndexesTraffic=todayIndexesMapper.getPartTraffic(todayIndexes);
		TodayIndexes todayIndexesPos=todayIndexesMapper.getPartPos(todayIndexes);
		TodayIndexes todayIndexesPv=todayIndexesMapper.getPv(todayIndexes);
		TodayIndexes todayIndexesUv=todayIndexesMapper.getUv(todayIndexes);
		TodayIndexes todayIndexesWeather=todayIndexesMapper.getWeather(todayIndexes);
		if(todayIndexesTraffic!=null){
			todayIndexes.setTraffic(todayIndexesTraffic.getTraffic());
			todayIndexes.setLastDayTraffic(todayIndexesTraffic.getLastDayTraffic());
			todayIndexes.setLastWeekTraffic(todayIndexesTraffic.getLastWeekTraffic());
		}
		else
		{
			todayIndexes.setTraffic(0);
			todayIndexes.setLastDayTraffic(0);
			todayIndexes.setLastWeekTraffic(0);
		}
		if(todayIndexesPos==null){
			todayIndexes.setPos(0);
			todayIndexes.setLastDayPos(0);
			todayIndexes.setLastWeekPos(0);
		}
		else
		{
			todayIndexes.setPos(todayIndexesPos.getPos());
			todayIndexes.setLastDayPos(todayIndexesPos.getLastDayPos());
			todayIndexes.setLastWeekPos(todayIndexesPos.getLastWeekPos());
		}
		if(todayIndexesPv==null){
			todayIndexes.setSinglePv(0);
			todayIndexes.setLastDaySinglePv(0);
			todayIndexes.setLastWeekSinglePv(0);
		}
		else{
			todayIndexes.setSinglePv(todayIndexesPv.getSinglePv());
			todayIndexes.setLastDaySinglePv(todayIndexesPv.getLastDaySinglePv());
			todayIndexes.setLastWeekSinglePv(todayIndexesPv.getLastWeekSinglePv());
		}
		if(todayIndexesUv==null){
			todayIndexes.setSigleUv(0);
			todayIndexes.setLastDaySigleUv(0);
			todayIndexes.setLastWeekSigleUv(0);
		}
		else{
			todayIndexes.setSigleUv(todayIndexesUv.getSigleUv());
			todayIndexes.setLastDaySigleUv(todayIndexesUv.getLastDaySigleUv());
			todayIndexes.setLastWeekSigleUv(todayIndexesUv.getLastWeekSigleUv());
		}
		todayIndexes.setTodayWeather(todayIndexesWeather.getTodayWeather());
		todayIndexes.setLastDayWeather(todayIndexesWeather.getLastDayWeather());
		todayIndexes.setLastWeekWeather(todayIndexesWeather.getLastWeekWeather());
		}catch(Exception e){
			e.printStackTrace();
		}
		return todayIndexes; */
		
	}
	public String getSiteNameBySiteId(String siteId)throws Exception{
		SiteAllInfo site=footfallGlobalMapper.selectSiteAllInfoById(siteId);
		return site.getSitename();
	}
	@SuppressWarnings("deprecation")
	public String getDate() throws Exception{
		Date date=new Date();
		DateFormat format1=new SimpleDateFormat("yyyy-MM-dd");
		String str=format1.format(date);
		int hh=date.getHours();
		if(hh<12){
			str+=" 上午";
		}
		else
		{
			str+=" 下午";
		}
		return str;
	}
	
	public HashMap<String, Object> getTodayIndexesJituanAll(String siteId,List<String> indexListCurrent) throws Exception {
		
		List<String> indexList = indexListCurrent;
		List<HashMap<String, Object>> indexDataList = new ArrayList<HashMap<String,Object>>();
		for(int i = 0;i<indexList.size();i++){
			HashMap<String,Object>  calret= footfallGlobalService.getCalret(Integer.parseInt(indexList.get(i)));
			String index = footfallGlobalService.getOrderField(indexList.get(i));
			String indexName = footfallGlobalService.getOrderFieldName(indexList.get(i));
			HashMap<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("index", index);
			paraMap.put("siteId", siteId);
			paraMap.put("calret", calret.get("calret"));
			paraMap.put("calretC", calret.get("calretC"));
			paraMap.put("calretD", calret.get("calretD"));
			
			String  dayData  = todayIndexesMapper.getIndexCurDayDataJituanAll(paraMap);
			String  lastDayData  = todayIndexesMapper.getIndexCurLastDayDataJituanAll(paraMap);
			String  lastWeekData  = todayIndexesMapper.getIndexCurLastWeekDataJituanAll(paraMap);
			String  lastDay  = todayIndexesMapper.getIndexCurLastDayJituanAll(paraMap);//昨天同一时间
			String  lastWeek  = todayIndexesMapper.getIndexCurLastWeekJituanAll(paraMap);//上周同一时间
			
			float lastDayFloat = lastDay !=null?Float.parseFloat(lastDay):0;
			float lastWeekFloat = lastWeek !=null?Float.parseFloat(lastWeek):0;
			float dayDataFloat = dayData !=null?Float.parseFloat(dayData):0;
			String comLastDay = "";
			String comLastWeekDay = "";
			if(lastDayFloat!=0){
				if(lastDayFloat>dayDataFloat){
					comLastDay = comLastDay+"-";
				}else{
					comLastDay = comLastDay+"+";
				}
				int per1 = (int) ((dayDataFloat-lastDayFloat)*100/lastDayFloat);
				if(per1<0){
					per1 = 0 - per1;
				}
				comLastDay = comLastDay+String.valueOf(per1);
			}else{
				comLastDay ="--";
			}
			if(lastWeekFloat!=0){
				if(lastWeekFloat>dayDataFloat){
					comLastWeekDay = comLastWeekDay+"-";
				}else{
					comLastWeekDay = comLastWeekDay+"+";
				}
				int per1 = (int) ((dayDataFloat-lastWeekFloat)*100/lastWeekFloat);
				if(per1<0){
					per1 = 0 - per1;
				}
				comLastWeekDay = comLastWeekDay+String.valueOf(per1);
			}else{
				comLastWeekDay ="--";
			}
			
			TodayIndexes IndexesData = new TodayIndexes();
			if(dayData!=null&&!"".equals(dayData)){
				IndexesData.setDayData(dayData);
			}else{
				IndexesData.setDayData("0");
			}
			if(lastDayData!=null&&!"".equals(lastDayData)){
				IndexesData.setLastDayData(lastDayData);
			}else{
				IndexesData.setLastDayData("0");
			}
			if(lastWeekData!=null&&!"".equals(lastWeekData)){
				IndexesData.setLastWeekData(lastWeekData);
			}else{
				IndexesData.setLastWeekData("0");
			}
			
			if(IndexesData == null){
				IndexesData =  new TodayIndexes();
				IndexesData.init();
			}
			HashMap<String, Object> result = new HashMap<String, Object>();
			result.put("indexNum", indexList.get(i));
			result.put("index", index);
			result.put("indexName", indexName);
			result.put("IndexesData", IndexesData);
			result.put("comLastDay", comLastDay);
			result.put("comLastWeekDay", comLastWeekDay);
			indexDataList.add(result);
		}
		TodayIndexes todayIndexes=new TodayIndexes();
		todayIndexes.setSiteId(siteId);
		TodayIndexes todayIndexesWeather=todayIndexesMapper.getWeather(todayIndexes);
		todayIndexes.setTodayWeather(todayIndexesWeather.getTodayWeather());
		todayIndexes.setLastDayWeather(todayIndexesWeather.getLastDayWeather());
		todayIndexes.setLastWeekWeather(todayIndexesWeather.getLastWeekWeather());
		HashMap<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("indexDataList", indexDataList);
		resultMap.put("todayIndexes", todayIndexes);
		return resultMap;
		
	}
	
}
